package com.wing.res.controller;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.res.model.response.ResProductDrawPicVo;
import com.wing.res.service.ResProductDrawPicService;
import com.wing.web.excel.DownExcelUtil;
import com.wing.web.excel.DrawPicExcelIn;
import com.wing.web.excel.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;


/**
 * sku图纸信息
 **/
@Slf4j
@Validated
@Api(value = "产品图纸信息", tags = "#/res/draw/proDrawPic")
@RestController("ResProductDrawPicController")
@RequestMapping("res/admin/proDrawPic")
public class ResProductDrawPicController {

    /**
     * 图纸表
     */
    @Resource
    private ResProductDrawPicService resProductDrawPicService;

    /**
     *查看图纸 以及他的历史版本
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param name
     * @return
     */
    @ApiOperation(value = "查看sku图纸列表", notes = "搜索-图纸")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ResProductDrawPicVo> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order

            ,String name

    ) {
        Page<ResProductDrawPicVo> pg = new Page<>(page, limit);
        Map<String,Object> map = new HashMap();

        map.put("name", name);


        Page<ResProductDrawPicVo> list = resProductDrawPicService.getListByMap(pg,map);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }





    /**
     * 下载物料录入excel模板
     */
    @ApiOperation(value = "下载物资类别录入excel模板")
    @GetMapping("/template")
    public void template(HttpServletResponse response, HttpServletRequest request) throws IOException {

        String[] headerArr = {"名称(*)", "图片地址(*)", "备注"};
        List<String[]> downData = new ArrayList<>();
        List<String> indexArr = new ArrayList<>();

        DownExcelUtil.createExcelTemplate(response, request, "产品图纸导入模板", Arrays.asList(headerArr), downData, indexArr);
    }



    /**
     * 导入excel
     */
    @ApiOperation(value = "导入excel")
    @PostMapping("/importExcel")
    public JsonResult importExcel(HttpServletRequest request, @RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return JsonResult.fail("导入文件为空！");
        }
        try {
            //获取原始的文件名
            String originalFilename = file.getOriginalFilename();
            String fileType = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());
            ExcelUtil<DrawPicExcelIn> reader = new ExcelUtil<>();
            InputStream is = file.getInputStream();
            List<DrawPicExcelIn> list = reader.read(is, DrawPicExcelIn.class);
            if (CollectionUtils.isEmpty(list)) {
                return JsonResult.fail("请在表格中添加数据！");
            }
            resProductDrawPicService.importExcel(list);
        } catch (BusinessException e) {
            log.error("导入文件异常！异常信息:", e);
            return JsonResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("导入文件异常！异常信息:", e);
            return JsonResult.exception("导入文件异常！异常信息:" + e.getMessage());
        }
        return JsonResult.success("导入成功！");
    }

}
